package project.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import project.model.History;
import project.model.Question;
import project.service.HistoryService;
import project.service.QuestionService;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RequestMapping(value = "/history")
@RestController
public class HistoryController {
    @Autowired
    HistoryService historyService;
    @Autowired
    QuestionService questionService;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping
    public void addHistory(History history) {
        System.out.println("History added");

        history.setCreate_time(String.valueOf(LocalDateTime.now()));
        historyService.addHistory(history);

    }
    @GetMapping
//    public List<History> getHistory(String uid) {
//        System.out.println("History get");
//        Integer idInt =uid==null?0:Integer.parseInt(uid);
//        return historyService.getHistory(idInt);
//    }

//    public List<Map<String, Object>> getHistory(String uid) {
//        System.out.println("History get");
//        Integer idInt =uid==null?0:Integer.parseInt(uid);
//        List<Map<String, Object>> history = new ArrayList<>();
//        //将history和题目存入map数组
//        for (History h : historyService.getHistory(idInt)) {
//            Map<String, Object> map = new HashMap<>();
//            Integer tid = h.getTid();
//            Question question = questionService.getQuestionById(tid);
//
//            map.put("hid", h.getHid());
//            map.put("tid", tid);
//            map.put("result", h.getResult());
//            map.put("create_time", h.getCreate_time());
//            if (question != null) {
//                map.put("title", question.getTitle());
//            } else {
//                map.put("title", "题目不存在或已被删除");  // 提供默认值
//                // 或者记录日志：logger.warn("题目ID {} 不存在", tid);
//            }
//            history.add(map);
//        }
//
//        return history;
//    }
    public List<Map<String, Object>> getHistory(@RequestParam String loginName) {
        System.out.println("History get");
        Integer idInt = (Integer) redisTemplate.opsForHash().get(loginName, "uid");
        if (idInt != null) {
            List<Map<String, Object>> history = new ArrayList<>();
            //将history和题目存入map数组
            for (History h : historyService.getHistory(idInt)) {
                Map<String, Object> map = new HashMap<>();
                Integer tid = h.getTid();
                Question question = questionService.getQuestionById(tid);

                map.put("hid", h.getHid());
                map.put("tid", tid);
                map.put("result", h.getResult());
                map.put("create_time", h.getCreate_time());
                if (question != null) {
                    map.put("title", question.getTitle());
                } else {
                    map.put("title", "题目不存在或已被删除");  // 提供默认值
                    // 或者记录日志：logger.warn("题目ID {} 不存在", tid);
                }
                history.add(map);
            }

            return history;
        }else return null;

    }
    @DeleteMapping
    public int deleteHistory(String hid) {
        System.out.println("History delete");
        Integer idInt =hid==null?0:Integer.parseInt(hid);
        return historyService.deleteHistory(idInt);
    }
}
